/*
 * @Author: error: git config user.name && git config user.email & please set dead value or install git
 * @Date: 2022-11-05 00:29:40
 * @LastEditors: error: git config user.name && git config user.email & please set dead value or install git
 * @LastEditTime: 2022-11-11 13:51:14
 * @FilePath: \WYW_0419_Oasis1\JavaScripts\ui\UIGame.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { DataCenterC, ModuleManager, UI } from "odin";
import { Consts } from "../Consts";
import { CoinData } from "../modules/coin/CoinData";
import { WeaponModuleC } from "../modules/weapon/WeaponModule";
import { UI_GameUI, UI_UIDie, UI_UIStart } from "./UITemplate";
/**
 * @Author: zhaolei
 * @Date: 2022-07-29 17:24:45
 * @LastEditors: zhaolei
 * @Description: 继承的UI_GameUI是UI生成工具自动生成的类，里面有UI上的所有控件
 * 这个UI主要负责开火按钮，跳跃按钮，以及boss的血量展示
 * 注意：不要改动UI_GameUI的内容，自动生成工具会覆盖UI_GameUI中的所有改动
 */
export class UIGame extends UI_GameUI {
	/**
	 * 武器客户端模块
	 */
	private _weaponModule: WeaponModuleC;
	/**
	 * 金币数据对象
	 */
	private coinData: CoinData;
	/**
	 * 只会在UI创建时调用一次，之后每次展示只会调用onShow方法
	 */
	public override onStart() {
		this.jumping.setFocusable(false);
		this.jumping.onClicked().add(() => {
			GamePlay.getCurrentPlayer().character.jump();
		});

		//点击开始，游戏开始，进行倒计时(出现bug)
		this.kaiShi.onClicked().add(() => {
			this.fengmian.setVisibility(MWGameUI.ESlateVisibility.Hidden);
			this.fengmian.destroyObject();
			this.kaiShi.setVisibility(MWGameUI.ESlateVisibility.Hidden);
			this.kaiShi.destroyObject();
			for(let i=300;i<0;i--)
			{
				setTimeout(() => {
					this.shijian.setText(i.toString());
				}, 1000);
			}
			
		});

		this.flying.setFocusable(false);
		this.flying.onClicked().add(() => {
			GamePlay.getCurrentPlayer().character.switchToFlying();
			GamePlay.getCurrentPlayer().character.brakingDecelerationFlying=500;
			GamePlay.getCurrentPlayer().character.maxFlySpeed=1000;
				setTimeout(()=>{
					GamePlay.getCurrentPlayer().character.switchToWalking();
				},7000)
		});

		this.yinshen.setFocusable(false);
		this.yinshen.onClicked().add(() => {
			GamePlay.getCurrentPlayer().character.isVisible=true;
					setTimeout(() => {
							GamePlay.getCurrentPlayer().character.isVisible=false;
					}, 4000);
		});

		// 摇杆控件，负责开火摇杆相关的逻辑
		this.qiang.setInActiveOpacity(1);
		this.qiang.setControlType(MWGameUI.EMWControlType.CameraType);
		this.qiang.onJoyStickDown().add(() => {
			// 调用武器模块方法开始开火，具体的开火逻辑由武器模块维护，UI中只负责调用
			if (!this._weaponModule) {
				this._weaponModule = ModuleManager.instance.getModule(WeaponModuleC);
			}
			this._weaponModule.startShoot();
		});
		this.qiang.onJoyStickUp().add(() => {
			// 调用武器模块方法停止开火
			this._weaponModule.stopShoot();
		});

		// 通过数据中心获取金币数量，更新到UI显示
		this.coinData = DataCenterC.instance.getModuleData(CoinData);
		this.updateCoinText();

		// 监听数据中心的数据改变，实时更新UI显示
		this.coinData.onDataChange.add(this.updateCoinText, this);

		this.setBossHpView(false);

		//监听死亡触发器（未完善）
		const trigger = MWCore.GameObject.find(Consts.MONSTER_ENTER_TRIGGER) as GamePlay.Trigger;
		trigger.onEnter.add((gameObject) => {
			if (GamePlay.isCharacter(gameObject)) {
				const player = (gameObject as GamePlay.Character).player;
				let character=player.character;
				character.ragdoll(true);
			}
		});
	}

	private updateCoinText() {
		this.mGoldCount.setText(this.coinData.count.toString());
	}

	/**
	 * 显示时就会被调用，因为UIGame是单例，所以onShow里的逻辑要确保每次展示都需要执行
	 */
	public override onShow() {
	}

	/**
	 * 销毁时移除数据中心监听，否则会导致UI释放后还进行更新，报错
	 */
	public override onDestroy() {
		super.onDestroy();
		this.coinData.onDataChange.remove(this.updateCoinText, this)
	}

	/**
	 * 更新boss血量,当boss血量小于0，弹出胜利界面
	 * @param bossHp 当前血量（替换为玩家当前血量）
	 */
	public updateMonsterHp(bossHp: number) {
		this.xuetiao.setCurrentValue(bossHp / 100);
		if (bossHp <= 0) {
			UI.instance.showPanel(UI_UIDie);
		}
	}

	public setBossHpView(isView: boolean) {
		this.xuetiao.setVisibility(isView ? MWGameUI.ESlateVisibility.HitTestInvisible : MWGameUI.ESlateVisibility.Hidden);
	}
}
